#include "userchangedialog.h"
#include "ui_userchangedialog.h"
#include"mes.h"
#include<QMessageBox>

UserChangeDialog::UserChangeDialog(QWidget *parent,QString NO,QString workNumber) :
    QDialog(parent),
    ui(new Ui::UserChangeDialog)
{
    ui->setupUi(this);
    this->NO=NO;
    this->workNumber=workNumber;
    setWindowTitle("保管人变更");
    ui->noticeLabel->setText("注意：保管人变更将导致设备的点检历史清空！");
    ui->oldWorkNumber->setText(workNumber);
    setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    on_oldWorkNumber_textEdited(workNumber);
}

UserChangeDialog::UserChangeDialog(QWidget *parent,QString NO,QString workNumber,QString checkerWorkNumber,bool checkUserFlag) :
    QDialog(parent),
    ui(new Ui::UserChangeDialog)
{
    ui->setupUi(this);
    this->checkUserFlag=checkUserFlag;
    this->NO=NO;
    this->workNumber=workNumber;
    setWindowTitle("点检人变更");
    if(checkerWorkNumber.trimmed()==""){
        ui->noticeLabel->setText("该设备尚未添加点检计划，无法变更点检人！");
        ui->pushButton->setEnabled(false);
    }else{
        ui->noticeLabel->setText("注意：设备的点检、校验、保养负责人将同时变更！");
    }
    ui->oldWorkNumber->setText(checkerWorkNumber);
    ui->oldNumberLabel->setText("*旧的点检人工号");
    ui->newNumberLabel->setText("*新的点检人工号");
    setWindowFlags(windowFlags()&~Qt::WindowContextHelpButtonHint);
    on_oldWorkNumber_textEdited(checkerWorkNumber);
}

UserChangeDialog::~UserChangeDialog()
{
    delete ui;
}

void UserChangeDialog::initOldName()
{
    if(Mes::getInstance()->getName(ui->oldName->text().trimmed(),oldName)){
        ui->oldName->setText(oldName);
    }else{
        ui->oldName->setText("工号不存在");
    }
}

void UserChangeDialog::on_oldWorkNumber_textEdited(const QString &arg1)
{
    if(Mes::getInstance()->getName(arg1,oldName)){
        ui->oldName->setText(oldName);
    }else{
        ui->oldName->setText("工号不存在");
    }
}

void UserChangeDialog::on_newWorkNumber_textEdited(const QString &arg1)
{
    if(Mes::getInstance()->getName(arg1,newName)){
        ui->newName->setText(newName);
    }else{
        ui->newName->setText("工号不存在");
    }
}

void UserChangeDialog::userSubmit()
{
    QString newWorkNumber=ui->newWorkNumber->text().trimmed();
    QString reasonText=ui->textEdit->toPlainText();
    if(newWorkNumber=="" || reasonText.trimmed()==""){
        ui->msg->setText("带*号项目不能为空");
        return;
    }

    if(newWorkNumber==ui->oldWorkNumber->text().trimmed()){
        ui->msg->setText("新保管人与旧保管人不能为同一人");
        return;
    }

    if(Mes::getInstance()->userChange(NO,workNumber,newWorkNumber,reasonText)){
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
        accept();
    }
}

void UserChangeDialog::checkSubmit()
{
    QString newWorkNumber=ui->newWorkNumber->text().trimmed();
    QString reasonText=ui->textEdit->toPlainText();
    if(newWorkNumber=="" || reasonText.trimmed()==""){
        ui->msg->setText("带*号项目不能为空");
        return;
    }

    if(newWorkNumber==ui->oldWorkNumber->text().trimmed()){
        ui->msg->setText("新点检人与旧点检人不能为同一人");
        return;
    }

    if(Mes::getInstance()->checkChange(NO,workNumber,newWorkNumber,reasonText)){
        QMessageBox::information(NULL,QString("提示"),QString("提交成功        "));
        accept();
    }
}

void UserChangeDialog::on_pushButton_clicked()
{
    if(checkUserFlag){
        checkSubmit();
    }else{
        userSubmit();
    }
}

void UserChangeDialog::on_pushButton_2_clicked()
{
    reject();
}
